{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 02\n",
    "\n",
    "# 向量内积\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea7e8c8a-4f2b-4710-8b22-80144e0194fe",
   "metadata": {},
   "source": [
    "该代码计算了行向量和列向量的点积，展示了向量间的内积（点积）及张量积的应用。点积（内积）是线性代数中基本的运算，用于将两个向量相乘并得到一个标量，而张量积则展示向量之间更复杂的组合关系。\n",
    "\n",
    "### 代码细节\n",
    "\n",
    "1. **行向量点积**：\n",
    "   - 定义行向量 $a\\_\\text{row} = [1, 2, 3]$ 和 $b\\_\\text{row} = [4, 3, 2]$。\n",
    "   - 使用`np.inner`计算两个行向量的点积，点积公式为：\n",
    "\n",
    "     $$\n",
    "     a\\_\\text{dot\\_b} = a\\_\\text{row} \\cdot b\\_\\text{row} = 1 \\times 4 + 2 \\times 3 + 3 \\times 2 = 16\n",
    "     $$\n",
    "\n",
    "   - 使用`np.sum(a_row * b_row)`逐元素相乘后求和，得到相同结果$16$。\n",
    "\n",
    "2. **列向量点积**：\n",
    "   - 定义列向量 $a\\_\\text{col} = \\begin{bmatrix} 1 \\\\ 2 \\\\ 3 \\end{bmatrix}$ 和 $b\\_\\text{col} = \\begin{bmatrix} -1 \\\\ 0 \\\\ 1 \\end{bmatrix}$。\n",
    "   - 使用`np.inner`计算列向量的张量积（tensor product），将$a_\\text{col}$和$b_\\text{col}$的每个元素成对相乘，结果为一个矩阵。\n",
    "   - 使用`np.sum(a_col * b_col)`逐元素乘积并求和得到一个标量，计算公式为：\n",
    "\n",
    "     $$\n",
    "     \\text{sum} = (1 \\times -1) + (2 \\times 0) + (3 \\times 1) = 2\n",
    "     $$\n",
    "\n",
    "### 总结\n",
    "\n",
    "该代码展示了如何计算行向量和列向量的点积及张量积。行向量的点积结果是一个标量，表示向量在相同维度方向上的投影和；列向量张量积的结果是矩阵，显示出各个元素的相互组合。点积和张量积广泛用于线性代数、机器学习和物理计算中，用于表示向量间的关系和相互作用。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89a0e5f4-cafd-4a96-b866-38927e551f25",
   "metadata": {},
   "source": [
    "## 行向量点积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bdb69cbd-7d5e-477e-be68-5a0e6073260a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 导入numpy库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f95e126b-e41c-4876-871c-db10731ee2c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "a_row = np.array([[1, 2, 3]])  # 定义行向量a_row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6adb3f32-6f23-45bd-9bd9-e33123aa7220",
   "metadata": {},
   "outputs": [],
   "source": [
    "b_row = np.array([[4, 3, 2]])  # 定义行向量b_row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b32994cd-2d72-486e-a86e-8467e5178284",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[16]]\n"
     ]
    }
   ],
   "source": [
    "a_dot_b = np.inner(a_row, b_row)  # 计算行向量a_row和b_row的点积\n",
    "print(a_dot_b)  # 输出点积结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8de71bf9-8452-4f74-b175-e5c975b9d8c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[16]]\n"
     ]
    }
   ],
   "source": [
    "print(np.inner(a_row[:], b_row[:]))  # 使用inner函数计算点积并输出结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "db0a44d1-2112-489c-af71-4d8fc4fc1f20",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16\n"
     ]
    }
   ],
   "source": [
    "print(np.sum(a_row * b_row))  # 使用逐元素乘积求和计算点积并输出结果"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f385a20-14d5-4acd-b6da-309affa619d2",
   "metadata": {},
   "source": [
    "## 列向量点积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f32ff632-b92e-4be2-8c3f-d4a5ef6579a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "a_col = np.array([[1], [2], [3]])  # 定义列向量a_col\n",
    "b_col = np.array([[-1], [0], [1]])  # 定义列向量b_col"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "24021839-ffb0-4475-bb93-2acacd90c637",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_col.T @ b_col"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78535016-9db7-407d-b707-3f63f75350d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(np.sum(a_col * b_col))  # 使用逐元素乘积求和计算并输出结果"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5bf115af-be57-494d-aa5f-2a68f1f63bd2",
   "metadata": {},
   "source": [
    "## 向量张量积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "057ccfd8-9770-44cd-a3cd-0ab21e5e24cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "a_dot_b = np.inner(a_col, b_col)  # 计算列向量a_col和b_col的点积（张量积）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "342c57d8-2c7e-45b0-b524-77b37bbb76b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1  0  1]\n",
      " [-2  0  2]\n",
      " [-3  0  3]]\n"
     ]
    }
   ],
   "source": [
    "print(a_dot_b)  # 输出张量积结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "757b1ef2-c471-42a6-b300-7375674cafa1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1,  0,  1],\n",
       "       [-2,  0,  2],\n",
       "       [-3,  0,  3]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_col @ b_col.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
